package com.bussiness.system.service;

import com.bussiness.config.util.SystemProperties;
import com.bussiness.system.dao.MyAuthDao;
import com.bussiness.system.dao.MyDeptPostsDao;
import com.common.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-09 10:04
 **/
@Service
public class SourceService {
    @Autowired
    private MenuVoUtil menuVoUtil;

    @Autowired
    private MyDeptPostsDao myDeptPostsDao;

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private MyAuthDao myAuthDao;

    /**
     * 获取权限树
     * @return
     */
    public List<MenuVo> getMenuVo(long userId) throws Exception{
        List<Map<String, Object>> listValue = new ArrayList<>();

        if(userId==systemProperties.getRootId()){
            //系统管理员（所有权限）
            listValue = myAuthDao.selectSourceAuth();
        } else {
            listValue = myDeptPostsDao.listAuthByUserId(userId);
        }

        List<MenuVo> voList = new ArrayList<MenuVo>();
        //转换为列表
        Iterator<Map<String, Object>> iterator=listValue.iterator();
        while (iterator.hasNext()){
            Map<String, Object> mapValue = iterator.next();
            String id = String.valueOf(mapValue.get("id"));
            String name = String.valueOf(mapValue.get("name"));
            String icon = String.valueOf(mapValue.get("icon"));
            String url = mapValue.get("url")+"?btnPurid="+mapValue.get("id");
            String parentId = String.valueOf(mapValue.get("pid"));
            MenuVo menuVo = new MenuVo(id, name, icon, url, parentId);
            voList.add(menuVo);
        }
        MenuVo menuVo = menuVoUtil.getListMenuVo(voList, "0");

        return menuVo.getMenus();
    }
}
